package com.zb_study.smallchange.oop;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;

public class SmallChangeSysOOP {

    boolean loop = true;
    Scanner scanner = new Scanner(System.in);
    String key = "";

    double money = 0;
    double balance = 0;

    String place = "";

    String ex = "";

    Date date = null; //date 是 java.util.Date 类型，表示日期
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");

    String details = "-------------零钱通明细--------------";

    public void mainMenu() {
        do {
            System.out.println("===============零钱通菜单(OOP)===============");
            System.out.println("\t\t\t1 零钱通明细");
            System.out.println("\t\t\t2 收益入账");
            System.out.println("\t\t\t3 消费");
            System.out.println("\t\t\t4 退     出");
            System.out.print("请选择(1-4)：");

            key = scanner.next();

            switch (key) {
                case "1":
                    detail();
                    break;
                case "2":
                    income();
                    break;
                case "3":
                    pay();
                    break;
                case "4":
                    exit();
                    break;

                default:
                    System.out.println("输入错误，请重新输入(1-4):");

            }
        } while (loop);

        System.out.println("------------退出零钱通系统------------");

    }


    public void detail() {
        System.out.println(details);
    }

    public void income() {
        System.out.print("收益入账金额：");
        money = scanner.nextDouble();
        if (money <= 0) {
            System.out.println("收益入账金额应该大于 0 元");
            return;
        }
        balance += money;
        date = new Date(); //获取当前日期
        // System.out.println(sdf.format(date));
        details += "\n收益入账\t+" + money + "\t" + sdf.format(date) + "\t" + balance;
    }

    public void pay() {
        System.out.print("输入消费金额：");
        money = scanner.nextDouble();
        if (money <= 0 || money > balance) {
            System.out.println("消费金额应该在0-" + balance + "之间");
            return;
        }
        System.out.print("输入消费事物：");
        place = scanner.next();
        balance -= money;
        details += "\n" + place + "\t+" + money + "\t" + sdf.format(date) + "\t" + balance;
    }

    public void exit() {
        System.out.println("你确定要退出吗？y/n");
        while (true) {
            ex = scanner.next();
            if (ex.equals("y")) {
                loop = false;
                break;
            } else if (ex.equals("n")) {
                break;
            } else {
                System.out.println("输入错误，请重新输入(y/n)：");
            }
        }
    }

}
